package com.sc.byahi.user.client;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.sc.byahi.user.shared.ProfileMetadata;

public class SummaryLine extends Composite {

	private static SummaryLineUiBinder uiBinder = GWT.create(SummaryLineUiBinder.class);
	@UiField
	FlexTable table;

	interface SummaryLineUiBinder extends UiBinder<Widget, SummaryLine> {
	}

	public SummaryLine() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setData(List<Entry<Long, Map<String, Set<Long>>>> currIds) {
		Map<Long, String> names = AppController.get().getInitData().getProfileNames();
		table.setWidget(0, 0, new HTML("<h>Profile ID</h>"));
		table.setWidget(0, 1, new HTML("<h>Name</h>"));
		table.setWidget(0, 2, new HTML("<h>Sent</h>"));
		table.setWidget(0, 3, new HTML("<h>Accepted</h>"));
		table.setWidget(0, 4, new HTML("<h>On Hold</h>"));
		table.setWidget(0, 5, new HTML("<h>Received</h>"));
		table.setWidget(0, 6, new HTML("<h>Accepted</h>"));
		table.setWidget(0, 7, new HTML("<h>On Hold</h>"));
		int row = 1;
		for (Entry<Long, Map<String, Set<Long>>> entry : currIds) {
			Hyperlink profileId = new Hyperlink();
			profileId.setText(entry.getKey().toString());
			profileId.setTargetHistoryToken("my_profile_id" + ":" + entry.getKey());
			table.setWidget(row, 0, profileId);
			HTML profileName = new HTML();
			profileName.setText(names.get(entry.getKey()));
			table.setWidget(row, 1, profileName);

			Hyperlink interestSent = new Hyperlink();
			Hyperlink interestAccepted = new Hyperlink();
			Hyperlink interestOnHold = new Hyperlink();
			Hyperlink reqReceived = new Hyperlink();
			Hyperlink reqAccepted = new Hyperlink();
			Hyperlink reqOnHold = new Hyperlink();
			LinkedHashMap<String, Hyperlink> map = new LinkedHashMap<String, Hyperlink>();
			map.put(ProfileMetadata.IS, interestSent);
			map.put(ProfileMetadata.IA, interestAccepted);
			map.put(ProfileMetadata.IH, interestOnHold);
			map.put(ProfileMetadata.RR, reqReceived);
			map.put(ProfileMetadata.RA, reqAccepted);
			map.put(ProfileMetadata.RH, reqOnHold);

			int column = 2;
			for (Hyperlink link : map.values()) {
				link.setText("");
				table.setWidget(row, column++, link);
			}
			for (String linkType : map.keySet()) {
				Hyperlink link = map.get(linkType);
				int size = entry.getValue().get(linkType).size();
				link.setTargetHistoryToken(linkType + ":" + entry.getKey());
				if (size > 0) {
					link.setText(size + "");
					link.setStyleName("red-link");
				} else {
					link.setText("-");
				}
			}
			row++;
		}
	}

}
